<?php

namespace app\index\controller;

class PolicyFileInfo
{
    public function getPolicyFileInfo()
    {
        $return_data = array();

        $policyfileinfo = db('policyfileinfo');

        $resp = $policyfileinfo->select();

        if ($resp) {
            $return_data['error_code'] = 0;
            $return_data['msg'] = '查询成功';
            $return_data['data'] = $resp;
        } else {
            $return_data['error_code'] = 1;
            $return_data['msg'] = '查询无结果';
        }

        return json($return_data);
    }

    public function deletePolicyFile()
    {
        $return_data = array();
        if (!isset($_POST['id']) || !isset($_POST['fileName'])) {
            $return_data["error_code"] = 3;
            $return_data['msg'] = "参数缺省，请检查";
            return json($return_data);
        }
        $fileName = $_POST['fileName'];

        $policyfileinfo = db('policyfileinfo');
        $where = array();
        $where['id'] = $_POST['id'];
        $where['fileName'] = $_POST['fileName'];


        $fileUrl = "../public/policyFiles/" . $fileName;

        $deleteFileResult = unlink($fileUrl);
        if ($deleteFileResult) {
            $deleteDataResult = $policyfileinfo->where($where)->delete();
            if ($deleteDataResult) {
                $return_data['error_code'] = 0;
                $return_data['msg'] = '删除数据库记录和文件成功';
            } else {
                $return_data['error_code'] = 1;
                $return_data['msg'] = '删除删除文件成功，但数据库记录失败';
            }
        } else {
            $return_data['error_code'] = 2;
            $return_data['msg'] = '删除失败';
        }
        return json($return_data);
    }
}
